<template>
  <el-dialog
    :title="`${drawerProps.title}物理模型`"
    v-model="drawerVisible"
    :draggable="true"
    :overflow="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="30px"
    width="800px"
    :destroy-on-close="true"
  >
    <el-form
      ref="ruleFormRef"
      style="height: 70vh; overflow: hidden auto"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-row>
        <el-col :span="24">
          <group-title title="基本信息"></group-title>
          <el-row>
            <form-item title="中文名称" prop="cnName" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.cnName" clearable></el-input>
            </form-item>
          </el-row>
          <el-row>
            <form-item title="英文名称" prop="enName" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.enName" clearable></el-input>
            </form-item>
          </el-row>
          <el-row>
            <form-item title="描述" prop="description" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.description" type="textarea" :rows="3"></el-input>
            </form-item>
          </el-row>
          <el-row>
            <form-item title="业务描述" prop="businessDesc" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.businessDesc" type="textarea" :rows="3"></el-input>
            </form-item>
          </el-row>
          <el-row>
            <form-item title="所有者用户" prop="ownerUser" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.ownerUser" clearable></el-input>
            </form-item>
          </el-row>

          <group-title title="配置信息"></group-title>
          <el-row>
            <form-item title="业务领域" prop="businessDomain" :title-span="6" :item-span="18">
              <el-input v-model="drawerProps.row!.businessDomain" clearable></el-input>
            </form-item>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="PhysicalModelDrawer">
import { ref, reactive } from "vue";
import { ElNotification, FormInstance } from "element-plus";
import { Model } from "@/views/model/modeTypes";
import apiService from "@/api/services/apiService";
import groupTitle from "@/components/application/form/group-title.vue";
import formItem from "@/components/application/form/form-item.vue";
import { useUserStore } from "@/stores/modules/user";

const rules = reactive({
  cnName: [{ required: true, message: "请输入中文名称" }],
  enName: [{ required: true, message: "请输入英文名称" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<Model.PhysicalModel>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {},
  api: undefined,
  getTableList: undefined
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value?.validate(async valid => {
    if (!valid) return;
    try {
      if (drawerProps.value.title === "新增") {
        // 初始化创建人和创建时间
        drawerProps.value.row.create_at = new Date();
        drawerProps.value.row.create_user = useUserStore().userInfo._id;
      }

      if (drawerProps.value.title === "新增") {
        await apiService.save(drawerProps.value.tbName, drawerProps.value.row);
      } else {
        await apiService.updateById(drawerProps.value.tbName, drawerProps.value.row);
      }
      ElNotification({
        title: "温馨提示",
        message: "保存成功",
        type: "success"
      });
      drawerProps.value.getTableList && drawerProps.value.getTableList();
      drawerVisible.value = false;
    } catch (error) {
      console.error(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
